//
//  BrandData.m
//  BosidengIphone
//
//  Created by  on 12-1-3.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BrandData.h"
#import "AppDelegate.h"

@implementation BrandData
@synthesize brandID;
@synthesize name;
@synthesize logo;
@synthesize introduce;
@synthesize iPhoneImages;
@synthesize sort;
@synthesize modifyDate;

- (id)init {
    if (self = [super init]) {
        ;
    }
    return self;
}

- (void)dealloc {
    [brandID release];
    [name release];
    [logo release];
    [introduce release];
    [iPhoneImages release];
    [sort release];
    [modifyDate release];
    [super dealloc];
}

+ (NSString *)getAllItemsQueryString {
    NSString *query = [NSString stringWithFormat:@"WHERE language='%@' ORDER BY sort", [AppDelegate getLanguage]];
    return query;
}

+ (id)getItemByID:(NSString *)uid {
    return [BrandData findFirstByCriteria:[BrandData getItemQueryString:@"brand_i_d" value:uid]];
}

+ (id)getFirstItem {
    return [BrandData findFirstByCriteria:[BrandData getAllItemsQueryString]];
}

+ (NSArray *)getAllItems {
    return [BrandData findByCriteria:[BrandData getAllItemsQueryString]];
}

- (NSString *)description {
    return [NSString stringWithFormat:@"BrandData Item.%d, brandID=%@", [self pk], brandID];
}

- (void)setValue:(NSDictionary *)data {
    [super setValue:data];
    self.brandID = [data objectForKey:@"BrandID"];
    self.name = [data objectForKey:@"Name"];
    self.logo = [data objectForKey:@"Logo"];
    self.introduce = [data objectForKey:@"Introduce"];
    self.iPhoneImages = [self formatStringData:[data objectForKey:@"iPhoneImages"]];
    self.sort = [data objectForKey:@"Sort"];
    self.modifyDate = [data objectForKey:@"ModifyDate"];
}

- (BOOL)hasBrandImgs {
    return ((iPhoneImages != nil) && ![@" " isEqualToString:iPhoneImages]);
}

- (void)removeLogoImage {
    NSString *imagePath = [AppDelegate getCacheFilePathByUrl:logo];
    [AppDelegate deleteFile:imagePath];
}

- (void)removeAboutImages {
    NSArray *images = [iPhoneImages componentsSeparatedByString: @"|"];
    for (NSString *imgInfo in images) {
        NSArray *infos = [imgInfo componentsSeparatedByString:@","];
        NSString *imagePath = [AppDelegate getCacheFilePathByUrl:[infos objectAtIndex:0]];
        [AppDelegate deleteFile:imagePath];
    }
}

- (void)removeLocalImages {
    [self removeLogoImage];
    if ([self hasBrandImgs]) {
        [self removeAboutImages];
    }
}

- (UIFont *)getFont {
    return [UIFont systemFontOfSize:9];
}

- (NSInteger)getContentHeight {
    return [self getTextHeight:introduce cx:300 font:[self getFont]];
}

- (NSInteger)getLineNumbers {
    CGFloat height = [self getContentHeight];
    CGFloat lineHeight = [self getFont].lineHeight;
	return height / lineHeight;
}

@end
